Should the size be based on the current size of the frame? YES
The user can change the size and shape of the frame and expects the star to change size to match the new frame.
The size of the star should be based on the current width and height of the frame.
These might not be the same value.
The size should be based on whichever is smaller so the star will fit.
paintComponent
does this with an if statement:
public void paintComponent ( Graphics gr ) { int width = getWidth(); int height = getHeight(); int min; super.paintComponent( gr ); setBackground(Color.white); gr.setColor( Color.blue ); if ( height > width ) min = height; else min = width; drawStar( gr, width/2, height/2, min/4 ); }
The drawStar( Graphics gr, int x, int y, int size )
method draws a six-pointed star centered at (x, y)
.
Each line of the star is size
pixels long.
At what coordinates is the center of the panel of given width and height?